package com.hp.team.bookstore.web.catalog;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.hp.team.bookstore.model.catalog.Product;
import com.hp.team.bookstore.service.catalog.ProductService;
import com.hp.team.bookstore.web.IPageUrlConstants;

@Controller
@SessionAttributes(value = { "product", "products", "title" })
public class ProductController implements IPageUrlConstants {

	@Autowired
	private ProductService productService;

	@RequestMapping(value = "/new_product")
	public String showPageNewProduct(ModelMap model) {
		return PAGE_PRODUCT_NEW;
	}

	@RequestMapping(value = "/add_product")
	public String addProduct(ModelMap model, @RequestParam String name, @RequestParam String author,
			@RequestParam String isbn, @RequestParam String publisher, @RequestParam String image) {

		Product product = productService.addProduct(name, null, author, publisher, isbn, image);
		model.addAttribute("product", product);

		return PAGE_PRODUCT_DETAILS;
	}

	@RequestMapping(value = "/product_list")
	public String listProducts(ModelMap model) {
		List<Product> products = productService.getAllProducts();
		model.addAttribute("products", products);
		model.addAttribute("title", "All Books");
		return PAGE_PRODUCT_LIST;
	}

	@RequestMapping(value = "/recent")
	public String listRecentProducts(ModelMap model) {
		List<Product> products = productService.getAllProducts();
		model.addAttribute("products", products);
		model.addAttribute("title", "Recent Books");
		return PAGE_PRODUCT_LIST;
	}

	@RequestMapping(value = "/special")
	public String listSpecialProducts(ModelMap model) {
		List<Product> products = productService.getAllProducts();
		model.addAttribute("products", products);
		model.addAttribute("title", "Special Books");
		return PAGE_PRODUCT_LIST;
	}

	@RequestMapping(value = "/browse")
	public String listAllProducts(ModelMap model) {
		List<Product> products = productService.getAllProducts();
		model.addAttribute("products", products);
		model.addAttribute("title", "Browse Books");
		return PAGE_PRODUCT_LIST;
	}

	@RequestMapping(value = "/product_list_back")
	public String goBackAllProducts(@ModelAttribute("products") List<Product> products, ModelMap model) {
		model.addAttribute("products", products);
		return PAGE_PRODUCT_LIST;
	}

	@RequestMapping(value = "/product_details")
	public String showProductDetails(ModelMap model, @RequestParam String id) {
		long productId = Long.parseLong(id);
		Product product = productService.getProduct(productId);
		model.addAttribute("product", product);
		return PAGE_PRODUCT_DETAILS;
	}

	@RequestMapping(value = "/product_image")
	@ResponseBody
	public byte[] showProductImage(@RequestParam String id, ModelMap model) {
		long productId = Long.parseLong(id);
		Product product = productService.getProduct(productId);
		return product.getImage();
	}
}